//! PhotoUpload.debug.js
//

(function() {

Type.registerNamespace('PhotoUpload.Classes');

////////////////////////////////////////////////////////////////////////////////
// PhotoUploadViewer

PhotoUploadViewer = function PhotoUploadViewer(opts) {
    /// <param name="opts" type="PhotoUpload.Classes.PhotoUploadOptions">
    /// </param>
    /// <field name="_instance$3" type="PhotoUploadViewer" static="true">
    /// </field>
    /// <field name="_opts$3" type="PhotoUpload.Classes.PhotoUploadOptions">
    /// </field>
    /// <field name="_topBar$3" type="jQueryObject">
    /// </field>
    PhotoUploadViewer.initializeBase(this);
    if (PhotoUploadViewer._instance$3 != null) {
        PhotoUploadViewer._instance$3.closeEv(null);
    }
    PhotoUploadViewer._instance$3 = this;
    this._opts$3 = opts;
    Cluster.glassOff();
    this.element = $("<div id='PhotoUploadViewer' class='AboveHider'/>");
}
PhotoUploadViewer.Ev = function PhotoUploadViewer$Ev(e, opts) {
    /// <param name="e" type="jQueryEvent">
    /// </param>
    /// <param name="opts" type="Object">
    /// </param>
    var el = opts['Element'] || $(document.body);
    new PhotoUploadViewer(opts).buildTo(el);
}
PhotoUploadViewer._afterUploadCx$3 = function PhotoUploadViewer$_afterUploadCx$3(path) {
    /// <param name="path" type="String">
    /// </param>
    /// <returns type="Function"></returns>
    Nsb.Storage.setSession('xPath', path);
    return function() {
        var fullName = Uri.join(ThemeBase.appUri, (Nsb.Storage.getSession(ThemeBase.redirectDataKey))['fullName']);
        new Await().addAw(ImageManager.updateImageDataAw, fullName).sleep(500).loadAndDo(Uri.scripts('Scripts/PhotoUpload.js'), 'PhotoUploadViewer.Ev', { Path: Nsb.Storage.getSession('xPath') }).addDx(function() {
            Nsb.Storage.removeSession('xPath');
        }).commit();
    };
}
PhotoUploadViewer.UpdateDirImagesEv = function PhotoUploadViewer$UpdateDirImagesEv(e, opts) {
    /// <param name="e" type="jQueryEvent">
    /// </param>
    /// <param name="opts" type="Object">
    /// </param>
    var puo = opts;
    new Await().addAw(FileBlobBase.getDirAw, puo.Path, true).addDx(function(aw) {
        puo.DirMap = aw.get_result();
    }).addDl(function(aw) {
        var awx = new Await();
        var files = puo.DirMap.files;
        for (var i = 0; i < files.length; i++) {
            if (!(/^(@|tn\.|ms\.)/i).test(files[i])) {
                awx.addAw(ImageManager.updateImageDataAw, Uri.join(ThemeBase.appUri, puo.Path, files[i]));
            }
        }
        awx.commit(aw);
    }).addAw(FileBlobBase.retrieveBlobAw, ThemeYoshi.imagesPartitionKey, ThemeYoshi.imagesRowKey).addDl(function(aw) {
        var imgs = aw.get_result();
        var awx = new Await();
        var $dict1 = imgs;
        for (var $key2 in $dict1) {
            var p = { key: $key2, value: $dict1[$key2] };
            awx.addAw(ImageManager.updateImageDataAw, p.value.source);
        }
        awx.commit(aw);
    }).commit();
}
PhotoUploadViewer.prototype = {
    _opts$3: null,
    _topBar$3: null,
    
    buildTo: function PhotoUploadViewer$buildTo(el) {
        /// <param name="el" type="jQueryObject">
        /// </param>
        if (this.element != null) {
            this.element.empty();
        }
        new Await().addAw(ss.Delegate.create(this, this._getImgDbAw$3)).handleDx(Exceptions.blobLoadError, function(aw) {
            aw.set_result({});
        }).addAw(ss.Delegate.create(this, this._buildAw$3), el).commit();
    },
    
    _buildAw$3: function PhotoUploadViewer$_buildAw$3(awp, el) {
        /// <param name="awp" type="Await">
        /// </param>
        /// <param name="el" type="jQueryObject">
        /// </param>
        this.element.appendTo(el).position({ my: 'center top', at: 'center bottom', of: window.self, collision: 'fit fit', offset: '0 -300' });
        this.element.css({ bottom: 0 - document.body.scrollTop });
        this._topBar$3 = $("<div class='TopBar'/>").appendTo(this.element);
        this._reDraggerEv$3();
        $(document).bind('scroll.puv', ss.Delegate.create(this, function() {
            this.element.css({ bottom: 0 - document.body.scrollTop });
            this._reDraggerEv$3();
        })).bind('resize.puv', ss.Delegate.create(this, this._reDraggerEv$3));
        $("<button class='UploadBtn'/>").appendTo(this._topBar$3).html('add new image').click(ss.Delegate.create(this, this._askUploadEv$3));
        $("<div class='CancelBtn'/>").appendTo(this._topBar$3).html("<img src='" + Uri.img('Common/close20.png') + "' width='20' height='20' border='0'>").click(ss.Delegate.create(this, this.closeEv));
        $("<span class='Info'/>").appendTo(this._topBar$3).html('drag these images onto page above or use add button to add more images');
        var content = $("<div class='Content'/>").appendTo(this.element);
        var $dict1 = awp.get_result();
        for (var $key2 in $dict1) {
            var p = { key: $key2, value: $dict1[$key2] };
            var url = p.value.thumb || p.value.source;
            $("<img class='PhotoImage'/>").prependTo($("<div class='PhotoItem'/>").appendTo(content).append($("<div class='PhotoDelete'/>").html('\u2716').attr('title', 'delete this image from image database').click(this._delEvHandler$3(p.key))).append($("<div class='PhotoInfo'/>").html(String.format('{0}', p.value.width + ' x ' + p.value.height)))).attr('src', url).attr('width', Math.min(p.value.width, 100) + 'px');
        }
        awp.done();
    },
    
    _reDraggerEv$3: function PhotoUploadViewer$_reDraggerEv$3(e) {
        /// <param name="e" type="jQueryEvent">
        /// </param>
        var ofst = $(window).height() - (this._topBar$3.outerHeight() + parseInt(this.element.css('border-top-width')) + 10 - document.body.scrollTop);
        this.element.draggable({ handle: this._topBar$3, containment: [0,0,$(window).width(),ofst], axis: 'y', scroll: false, drag: ss.Delegate.create(this, function() {
            this.element.css({ bottom: 0 - document.body.scrollTop });
        }) });
    },
    
    _delEvHandler$3: function PhotoUploadViewer$_delEvHandler$3(url) {
        /// <param name="url" type="String">
        /// </param>
        /// <returns type="Function"></returns>
        return ss.Delegate.create(this, function(e) {
            new Await().addAw(ss.Delegate.create(new Ask(), new Ask().okCancelAw), { Msg: ThemeYoshi.forSure }).addAw(DomElement.requireResultAw, true).addDx(ss.Delegate.create(this, function() {
                this.deleteImageAw(Await.get_asyncAw(), url);
                $(e.target).closest('.PhotoItem').remove();
            })).commit();
        });
    },
    
    deleteImageAw: function PhotoUploadViewer$deleteImageAw(awp, url) {
        /// <param name="awp" type="Await">
        /// </param>
        /// <param name="url" type="String">
        /// </param>
        FileManager.updateFileAw(awp, ThemeYoshi.imagesPartitionKey, ThemeYoshi.imagesRowKey, function(imagesD) {
            var imgs = imagesD;
            var ii = imgs[url];
            if (ii != null) {
                if (ii.path != null) {
                    ImageManager.deleteImageAw(Await.get_asyncAw(), ii.path);
                }
                if (ii.thumb != null) {
                    ImageManager.deleteImageAw(Await.get_asyncAw(), ii.thumb);
                }
                delete imgs[url];
            }
        });
    },
    
    _askUploadEv$3: function PhotoUploadViewer$_askUploadEv$3(e) {
        /// <param name="e" type="jQueryEvent">
        /// </param>
        var fileName = 'ms.' + Formulas.get_newId();
        new Await().addAw(ss.Delegate.create(new Ask(), new Ask().fileUploadAw), { Msg: 'Upload an image', path: this._opts$3.Path, filename: fileName, password: Nsb.Storage.getLocal(ThemeYoshi.get_curatePasswordKey()), action: PhotoUploadViewer._afterUploadCx$3(this._opts$3.Path) }).commit();
    },
    
    closeEv: function PhotoUploadViewer$closeEv(e) {
        /// <param name="e" type="jQueryEvent">
        /// </param>
        PhotoUploadViewer._instance$3 = null;
        DomElement.cancelEvent(e);
        this.element.remove();
        $(document).unbind('.puv');
    },
    
    _getImgDbAw$3: function PhotoUploadViewer$_getImgDbAw$3(awp) {
        /// <param name="awp" type="Await">
        /// </param>
        new Await().addAw(FileBlobBase.retrieveBlobAw, ThemeYoshi.imagesPartitionKey, ThemeYoshi.imagesRowKey).commit(awp);
    }
}


////////////////////////////////////////////////////////////////////////////////
// PhotoUpload.Classes.PhotoUploadOptions

PhotoUpload.Classes.PhotoUploadOptions = function PhotoUpload_Classes_PhotoUploadOptions() {
    /// <field name="DirMap" type="DirMap">
    /// </field>
    /// <field name="Path" type="String">
    /// </field>
}
PhotoUpload.Classes.PhotoUploadOptions.prototype = {
    DirMap: null,
    Path: null
}


PhotoUploadViewer.registerClass('PhotoUploadViewer', Actor);
PhotoUpload.Classes.PhotoUploadOptions.registerClass('PhotoUpload.Classes.PhotoUploadOptions');
PhotoUploadViewer._instance$3 = null;
})();

//! This script was generated using Script# v0.7.4.0
